package com.app.biz.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.app.biz.domain.request.BizCityIndustrialEconomicsRequest;
import com.app.biz.mapper.BizCityIndustrialEconomicsFormalMapper;
import com.app.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizCityIndustrialEconomicsMapper;
import com.app.biz.domain.BizCityIndustrialEconomics;
import com.app.biz.service.IBizCityIndustrialEconomicsService;

/**
 * 分市工业企业主要经济指标Service业务层处理
 *
 * @author SangGuangCan
 * @date 2023-12-29
 */
@Service
public class BizCityIndustrialEconomicsServiceImpl implements IBizCityIndustrialEconomicsService
{

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final BizCityIndustrialEconomicsMapper bizCityIndustrialEconomicsMapper;

    private final BizCityIndustrialEconomicsFormalMapper bizCityIndustrialEconomicsFormalMapper;

    @Autowired
    public BizCityIndustrialEconomicsServiceImpl(BizCityIndustrialEconomicsMapper bizCityIndustrialEconomicsMapper,
                                                 BizCityIndustrialEconomicsFormalMapper bizCityIndustrialEconomicsFormalMapper){
        this.bizCityIndustrialEconomicsMapper = bizCityIndustrialEconomicsMapper;
        this.bizCityIndustrialEconomicsFormalMapper = bizCityIndustrialEconomicsFormalMapper;
    }

    /**
     * 查询分市工业企业主要经济指标
     *
     * @param baseId 分市工业企业主要经济指标主键
     * @return 分市工业企业主要经济指标
     */
    @Override
    public BizCityIndustrialEconomics selectBizCityIndustrialEconomicsByBaseId(String baseId)
    {
        return bizCityIndustrialEconomicsMapper.selectBizCityIndustrialEconomicsByBaseId(baseId);
    }

    /**
     * 查询分市工业企业主要经济指标列表
     *
     * @param bizCityIndustrialEconomics 分市工业企业主要经济指标
     * @return 分市工业企业主要经济指标
     */
    @Override
    public List<BizCityIndustrialEconomics> selectBizCityIndustrialEconomicsList(BizCityIndustrialEconomics bizCityIndustrialEconomics)
    {
        return bizCityIndustrialEconomicsMapper.selectBizCityIndustrialEconomicsList(bizCityIndustrialEconomics);
    }

    /**
     * 新增分市工业企业主要经济指标
     *
     * @param bizCityIndustrialEconomics 分市工业企业主要经济指标
     * @return 结果
     */
    @Override
    public int insertBizCityIndustrialEconomics(BizCityIndustrialEconomics bizCityIndustrialEconomics)
    {
        bizCityIndustrialEconomics.setBaseId(IdUtil.simpleUUID());
        bizCityIndustrialEconomics.setBaseCreateTime(DateUtils.getNowDate());

        return bizCityIndustrialEconomicsMapper.insertBizCityIndustrialEconomics(bizCityIndustrialEconomics);
    }

    /**
     * 修改分市工业企业主要经济指标
     *
     * @param bizCityIndustrialEconomics 分市工业企业主要经济指标
     * @return 结果
     */
    @Override
    public int updateBizCityIndustrialEconomics(BizCityIndustrialEconomics bizCityIndustrialEconomics)
    {
        return bizCityIndustrialEconomicsMapper.updateBizCityIndustrialEconomics(bizCityIndustrialEconomics);
    }

    /**
     * 批量删除分市工业企业主要经济指标
     *
     * @param baseIds 需要删除的分市工业企业主要经济指标主键
     * @return 结果
     */
    @Override
    public int deleteBizCityIndustrialEconomicsByBaseIds(String[] baseIds)
    {
        return bizCityIndustrialEconomicsMapper.deleteBizCityIndustrialEconomicsByBaseIds(baseIds);
    }

    /**
     * 删除分市工业企业主要经济指标信息
     *
     * @param baseId 分市工业企业主要经济指标主键
     * @return 结果
     */
    @Override
    public int deleteBizCityIndustrialEconomicsByBaseId(String baseId)
    {
        return bizCityIndustrialEconomicsMapper.deleteBizCityIndustrialEconomicsByBaseId(baseId);
    }

    /**
     * <p>
     * 查询数据库中数据的日期(year_month字段)并进行去重复操作<br/>
     * 主要用于的是APP端左上角的日期过滤
     * </p>
     *
     * @return 日期的数组
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizCityIndustrialEconomicsMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            return uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * <p>
     * 通过日期查询表格中的数据
     * </p>
     *
     * @param request 前端传传来的数据
     * @return 表格中的数据
     */
    @Override
    public List<BizCityIndustrialEconomics> getEconomicIndicatorsByDateList(BizCityIndustrialEconomicsRequest request) {
        String dates = request.getDates();
        DateTime parse = DateUtil.parse(dates, "yyyy年MM月");
        request.setDates(DateUtil.format(parse,"yyyy-MM-dd"));
        return bizCityIndustrialEconomicsMapper.getEconomicIndicatorsByDateList(request);
    }

    /**
     * @return
     */
    @Override
    public boolean transferDataInOneClick() {
        try{
//            /* 查询数据库中最新的一条日期 */
//            String latestDateStr = bizCityIndustrialEconomicsMapper.getLatestDateData();
//            BizCityIndustrialEconomics params = new BizCityIndustrialEconomics();
//            params.setYearMonth(DateUtils.parseDate(latestDateStr,"yyyy-MM-dd"));

            List<BizCityIndustrialEconomics> result = bizCityIndustrialEconomicsMapper.selectBizCityIndustrialEconomicsAllData();

            int count = 0;
            List<BizCityIndustrialEconomics> list = new ArrayList<>();
            for(BizCityIndustrialEconomics item : result){
                item.setBaseCreateTime(DateUtils.getNowDate());
                list.add(item);
                count++;
                if(count >= 100){
                    bizCityIndustrialEconomicsFormalMapper.batchInsertbBizCityIndustrialEconomicsFormalMapper(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                bizCityIndustrialEconomicsFormalMapper.batchInsertbBizCityIndustrialEconomicsFormalMapper(list);
                list.clear();
            }
            return true;
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return false;
        }
    }

    /**
     *
     */
    @Override
    public void clearAllTableData() {
        bizCityIndustrialEconomicsMapper.clearAllTableData();
    }
}
